二叉樹的基本遍歷

2021-10-07 01:36:56 字數 4474 閱讀 4325

#define _crt_secure_no_warnings

#include#include#includestruct binnode

;//先序遍歷 根左右

void xianxubianli(struct binnode *root)

printf("%c ", root->ch);

//遞迴遍歷左子樹

xianxubianli(root->lchlid);

xianxubianli(root->rchlid);

}//中序遍歷 左根右

void zhongxubianli(struct binnode *root)

zhongxubianli(root->lchlid);

printf("%c ", root->ch);

//遞迴遍歷左子樹

zhongxubianli(root->rchlid);

}//後序遍歷 左右根

void houxubianli(struct binnode *root)

houxubianli(root->lchlid);

//遞迴遍歷左子樹

houxubianli(root->rchlid);

printf("%c ", root->ch);

}void test()

; struct binnode nodeb = ;

struct binnode nodec = ;

struct binnode noded = ;

struct binnode nodee = ;

struct binnode nodef = ;

struct binnode nodeg = ;

struct binnode nodeh = ;

/* a b f

c g

d e h

*/nodea.lchlid = &nodeb;

nodea.rchlid = &nodef;

nodeb.rchlid = &nodec;

nodec.lchlid = &noded;

nodec.rchlid = &nodee;

nodef.rchlid = &nodeg;

nodeg.lchlid = &nodeh;

printf("\n");

xianxubianli(&nodea);

printf("\n");

printf("*****===\n");

zhongxubianli(&nodea);

printf("\n");

printf("*****===\n");

houxubianli(&nodea);

printf("\n");

printf("*****===\n");

}//葉子節點的個數

void yezi(struct binnode *root,int *num)

if (root->lchlid == null && root->rchlid == null)

yezi(root->lchlid,num);

yezi(root->rchlid,num);

}//樹的高度

int gaodu(struct binnode *root)

//左子樹高度

int lgao = gaodu(root->lchlid);

//右子樹高度

int rgao = gaodu(root->rchlid);

int gao = lgao > rgao ? lgao + 1 : rgao + 1;

return gao; }

//二叉樹拷貝

struct binnode *cp(struct binnode *root, struct binnode **shu)

//先拷貝左子樹

struct binnode *lchlid = cp(root->lchlid,shu);

//再拷貝右子樹

struct binnode *rchlid = cp(root->rchlid,shu);

struct binnode *node = malloc(sizeof(struct binnode));

node->lchlid = lchlid;

node->rchlid = rchlid;

node->ch = root->ch;

//strcpy(node->ch, root->ch);

*shu = node;

return *shu;

}//二叉樹拷貝

struct binnode *cp1(struct binnode *root)

//先拷貝左子樹

struct binnode *lchlid = cp1(root->lchlid);

//再拷貝右子樹

struct binnode *rchlid = cp1(root->rchlid);

struct binnode *node = malloc(sizeof(struct binnode));

node->lchlid = lchlid;

node->rchlid = rchlid;

node->ch = root->ch;

//strcpy(node->ch, root->ch);

return node;

}//釋放記憶體

void freetree(struct binnode *root)

freetree(root->lchlid);

freetree(root->rchlid);

printf("shu=%c ", root->ch);

free(root);

/*這是錯誤的 因為先釋放左邊後釋放右邊 即使左子樹和右子樹 都釋放後

他們的爸爸 還是指向他們 所以直接釋放就好

if ((root->lchlid == null) && (root->rchlid == null))

*/

}void test1()

; struct binnode nodeb = ;

struct binnode nodec = ;

struct binnode noded = ;

struct binnode nodee = ;

struct binnode nodef = ;

struct binnode nodeg = ;

struct binnode nodeh = ;

/* a

b f

c g

d e h

*/ nodea.lchlid = &nodeb;

nodea.rchlid = &nodef;

nodeb.rchlid = &nodec;

nodec.lchlid = &noded;

nodec.rchlid = &nodee;

nodef.rchlid = &nodeg;

nodeg.lchlid = &nodeh;

int num = 0;

yezi(&nodea,&num);

printf("\n");

//葉子節點個數

printf("葉子節點個數num=%d\n", num);

//樹的高度

int gao = gaodu(&nodea);

printf("樹的高度num=%d\n", gao);

struct binnode *newshu = malloc(sizeof(struct binnode));

cp(&nodea,&newshu);

//先序遍歷新拷貝的樹1

printf("先序遍歷新拷貝的樹1\n");

xianxubianli(newshu);

printf("\n");

printf("***************=\n");

struct binnode *newshu2 = cp1(&nodea);

//先序遍歷新拷貝的樹2

printf("先序遍歷新拷貝的樹2\n");

xianxubianli(newshu2);

printf("\n");

printf("***************=\n");

//這個不是拷貝的 無法釋放

//freetree(&nodea);

printf("*****樹1的釋放順序*****=\n");

freetree(newshu);

printf("\n");

printf("*****樹2的釋放順序*****=\n");

二叉樹的遍歷 二叉樹遍歷與儲存

在資料結構中,二叉樹是非常重要的結構。例如 資料庫中經常用到b 樹結構。那麼資料庫是如何去單個查詢或者範圍查詢?首先得理解二叉樹的幾種遍歷順序 先序 中序 後序 層次遍歷。先序 根節點 左子樹 右子樹 中序 左子樹 根節點 右子樹 後序 左子樹 右子樹 根節點 按層級 class node if c...

構建二叉樹 遍歷二叉樹

陣列法構建二叉樹 public class main public static void main string args 用陣列的方式構建二叉樹 public static void createbintree 把linkedlist集合轉成二叉樹的形式 for int j 0 j 最後乙個父節...

玩轉二叉樹(二叉樹的遍歷)

時間限制 400 ms 記憶體限制 65536 kb 長度限制 8000 b 判題程式 standard 作者 陳越 給定一棵二叉樹的中序遍歷和前序遍歷,請你先將樹做個鏡面反轉,再輸出反轉後的層序遍歷的序列。所謂鏡面反轉,是指將所有非葉結點的左右孩子對換。這裡假設鍵值都是互不相等的正整數。輸入格式 ...